package com.ffox.service.impl;

import com.ffox.entity.SchoolApproval;
import com.ffox.entity.SchoolInformation;
import com.ffox.fastDfsUtil.FastDfsUtil;
import com.ffox.jjwt.JwtUtils;
import com.ffox.mapper.SchoolLoginMapper;
import com.ffox.pinyin4j.ChineseToFirstLetter;
import com.ffox.result.Result;
import com.ffox.service.SchoolLoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;


import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 实现学校登录服务的类
 */
@Service
public class SchoolLoginServiceImpl implements SchoolLoginService {
    @Autowired
    SchoolLoginMapper schoolLoginMapper;
    @Autowired
    BCryptPasswordEncoder bCryptPasswordEncoder;
    @Autowired
    private FastDfsUtil fastDfsUtil;
    @Autowired
    ChineseToFirstLetter chineseToFirstLetter;

    /**
     * 学校用户登录方法
     *
     * @param number   学校用户账号
     * @param password 学校用户密码
     * @return 登录结果，包含成功或失败信息及用户数据
     */
    @Override
    public Result<Map<String, Object>> schoolLogin(String number, String password) {
        SchoolInformation datas = schoolLoginMapper.getSchoolByNumber(number);
        if (datas == null) {
            return Result.fail("账号不存在");
        }

        // 验证密码是否正确
        if (!bCryptPasswordEncoder.matches(password, datas.getPassword())) {
            return Result.fail("账号或密码错误");
        }

        // 更新登录时间和IP地址
        schoolLoginMapper.updateSchool(datas.getId(), new HashMap<String, Object>() {{
            System.out.println(LocalDateTime.now());
            put("lastLoginTime", LocalDateTime.now());
        }});

        List<String> roles = Arrays.asList("ROLE_SCHOOL");

        String token = JwtUtils.generateToken(number,roles);


        Map<String, Object> userInfo = new HashMap<>();
        userInfo.put("name", datas.getRealName());
        userInfo.put("avatar", datas.getAvatar());
        userInfo.put("role", "school");
        userInfo.put("number", number);

        Map<String, Object> result = new HashMap<>();
        result.put("userInfo", userInfo);
        result.put("token", token);

        return Result.success("登录成功！", result);
    }

    /**
     * 学校用户注册方法
     *
     * @param number        用户账号
     * @param password      用户密码
     * @param mobile        用户手机号
     * @param province      用户所在省份
     * @param city          用户所在城市
     * @param district      用户所在地区
     * @param detailAddress 用户详细地址
     * @return 注册结果，包含成功或失败信息及影响的行数
     */
    @Override
    public Result<Integer> schoolInsert(String number, String password, String districtCode, String realName, String mobile, String province, String city, String district, String detailAddress) {
        // 检查用户是否已存在
        if (schoolLoginMapper.getSchoolByNumber(number) != null) {
            return Result.fail("用户已存在");
        }

        SchoolApproval schoolApproval = new SchoolApproval();
        schoolApproval.setStatus(0);
        schoolApproval.setNumber(number);
        schoolApproval.setPassword(bCryptPasswordEncoder.encode(password));
        schoolApproval.setCode(districtCode+chineseToFirstLetter.getFirstLetter(realName));
        schoolApproval.setRealName(realName);
        schoolApproval.setMobile(mobile);
        schoolApproval.setProvince(province);
        schoolApproval.setCity(city);
        schoolApproval.setDistrict(district);
        schoolApproval.setDetailAddress(detailAddress);
        Integer result = schoolLoginMapper.insertSchool(schoolApproval);
        if (result > 0) {
            return Result.success("用户添加成功", result);
        } else {
            return Result.fail("用户添加失败");
        }
    }

    /**
     * 删除学校用户方法
     *
     * @param id 用户ID
     * @return 删除结果，包含成功或失败信息及影响的行数
     */
    @Override
    public Result<Integer> schoolDelete(Long id) {
        Integer result = schoolLoginMapper.updateSchool(id, new HashMap<String, Object>() {{
            put("status", 0);
        }});
        if (result == 1) {
            return Result.success("用户删除成功", result);
        } else {
            return Result.fail("用户删除失败");
        }

    }
}

